import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
import home from './home'
import medicalInstitution from './medicalInstitution'
import nursingPlatform from './nursingPlatform'
import careSurvey from './careSurvey'
import drugs from './drugs'
import twoMedical from './twoMedical'
import nurseDetail from './nurseDetail'

let originPush = VueRouter.prototype.push;

VueRouter.prototype.push = function(location,resolve,reject){
    if(resolve && reject){
      originPush.call(this,location,resolve,reject);
    }else{
      originPush.call(this,location,()=>{},()=>{});
    }
} 

const router = new VueRouter({
  mode: 'history',
  routes: [
    {path:"/",redirect:"home"},
    { 
      name:'login',
      path: "/login",
      meta: {
        title: "登录"
      },
      component:()=>import("@/views/Login")
    },
    {
      path:'/register',
      meta:{
        title:'注册'
      },
      component:()=>import("@/views/Register")
    },
    { 
      name:'goMydetail',
      path:'/goMydetail',
      meta:{
        title:'个人资料'
      },
      component:()=>import("@/views/User/UserDetail")
    },
    { 
      name:'nurseRelease',
      path:'/nurseRelease',
      meta:{
        title:'护工发布'
      },
      component:()=>import("@/views/NursingPlatform/NurseRelease")
    },
    home,medicalInstitution,nursingPlatform,careSurvey,drugs,twoMedical,nurseDetail,
    {path:"/notfound",component:()=>import("@/views/404")},
    {path:"*",redirect:"notfound"}
  ]
})
const originalPush = VueRouter.prototype.push
 
/* VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
} */


router.beforeEach((to,from,next) => {
    if(to.meta.isAuth){
      if(JSON.parse(localStorage.getItem("isLogin"))){
          next()
      }else{
        alert('还未登录,请先登录')
        router.push('/login')
      }
    }else{
      next()
    }
})
router.afterEach((to,from) =>{
  document.title = to.meta.title || '医查通'
})

export default router;